package com.guli.edu.controller;


import com.guli.commonutils.R;
import com.guli.edu.entity.Chapter;
import com.guli.edu.entity.chapter.ChapterVo;
import com.guli.edu.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author liuzg
 * @since 2021-03-08
 */
@RestController
@RequestMapping("/edu/chapter")
@Api(tags = "章节管理")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "课程大纲列表,根据课程id进行查询")
    @GetMapping(value = "/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping(value = "/addChapter")
    public R addChapter(@RequestBody Chapter eduChapter) {
        chapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "根据章节id查询")
    @GetMapping(value = "/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        Chapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    @ApiOperation(value = "修改章节")
    @PostMapping(value = "/updateChapter")
    public R updateChapter(@RequestBody Chapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "删除的方法")
    @DeleteMapping(value = "/remove/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        boolean flag = chapterService.deleteChapter(chapterId);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }

}

